import _objectSpread from "@babel/runtime/helpers/esm/objectSpread2";
import _regeneratorRuntime from "@babel/runtime/regenerator";

/**
 * model - 工作台卡片
 * @date: 2019-02-23
 */
import { getResponse } from 'utils/utils';
import { queryFunctions as _queryFunctions, addFunctions as _addFunctions } from "../../services/commonlyUsedService";
export default {
  namespace: 'commonlyUsed',
  state: {
    // Cpaas
    functions: [],
    // 固定的常用功能
    allFunction: [],
    // 全部的常用功能
    checkedKeys: [],
    // 选中的常用功能
    allCheckedKeys: [],
    commonlyUsedLoading: true,
    // 常用功能预加载loading
    isListLoad: null
  },
  effects: {
    // 查询固定的常用功能
    queryFunctions: /*#__PURE__*/_regeneratorRuntime.mark(function queryFunctions(_ref, _ref2) {
      var payload, call, put, data;
      return _regeneratorRuntime.wrap(function queryFunctions$(_context) {
        while (1) {
          switch (_context.prev = _context.next) {
            case 0:
              payload = _ref.payload;
              call = _ref2.call, put = _ref2.put;
              _context.t0 = getResponse;
              _context.next = 5;
              return call(_queryFunctions);

            case 5:
              _context.t1 = _context.sent;
              data = (0, _context.t0)(_context.t1);

              if (!data) {
                _context.next = 10;
                break;
              }

              _context.next = 10;
              return put({
                type: 'updateState',
                payload: {
                  checkedKeys: data.map(function (item) {
                    return item.menuId;
                  }),
                  functions: data.splice(0, payload.maxNum)
                }
              });

            case 10:
              return _context.abrupt("return", data);

            case 11:
            case "end":
              return _context.stop();
          }
        }
      }, queryFunctions);
    }),
    // 添加需要展示的常用功能
    addFunctions: /*#__PURE__*/_regeneratorRuntime.mark(function addFunctions(_ref3, _ref4) {
      var payload, call, orderType;
      return _regeneratorRuntime.wrap(function addFunctions$(_context2) {
        while (1) {
          switch (_context2.prev = _context2.next) {
            case 0:
              payload = _ref3.payload;
              call = _ref4.call;
              _context2.t0 = getResponse;
              _context2.next = 5;
              return call(_addFunctions, payload);

            case 5:
              _context2.t1 = _context2.sent;
              orderType = (0, _context2.t0)(_context2.t1);
              return _context2.abrupt("return", getResponse(orderType));

            case 8:
            case "end":
              return _context2.stop();
          }
        }
      }, addFunctions);
    })
  },
  reducers: {
    updateState: function updateState(state, _ref5) {
      var payload = _ref5.payload;
      return _objectSpread(_objectSpread({}, state), payload);
    }
  }
};